package com.changfen.client.futures;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisFuture;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;

import java.util.concurrent.ExecutionException;
import java.util.function.Consumer;

/**
 * @author 86180
 * @Classname Future
 * @Description TODO
 * @Date 2022/9/5 23:02
 */
public class Future {
    public static void main(String[] args) {

        /*
           1.Trigger multiple invocations without the urge to wait for the predecessors (Batching)
           2.Invoking a command without awaiting the result at all (Fire&Forget)
           3.Invoking a command and perform other computing in the meantime (Decoupling)
           4.Adding concurrency to certain computational efforts (Concurrency)
        */

        RedisURI redisUri = RedisURI.builder().withHost("localhost").withPort(6379).withPassword("thingsmatrix@8".toCharArray()).build();

        RedisClient redisClient = RedisClient.create(redisUri);
        StatefulRedisConnection<String, String> connection = redisClient.connect();
        RedisAsyncCommands<String, String> asyncCommand = connection.async();

        RedisFuture<String> redisFuture = asyncCommand.get("feng");

        redisFuture.thenAccept(s -> System.out.println("get value" + s));

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        redisFuture.acceptEitherAsync(asyncCommand.get("a"),s-> System.out.println(s));
    }
}
